package com.nhjf.common.util.ssl;

import com.nhjf.common.util.HttpResponse;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.net.URLEncoder;
import java.util.Date;

public class HttpsClientUtil {
    public static HttpResponse reqForGet(String postUrl, Integer timeout) {
        HttpResponse res = new HttpResponse();
        Date sendTime = new Date();
        res.setSendTime(sendTime);
        try {
             HttpClient httpClient = new DefaultHttpClient();
            httpClient = SSLClient.getNewHttpsClient(httpClient);
            String html = "";
            HttpGet request = new HttpGet(postUrl);
            org.apache.http.HttpResponse response = null;
                response = httpClient.execute(request);
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity mEntity = response.getEntity();
                    html = EntityUtils.toString(mEntity,"utf-8");
                }
            res.setResult(html);
            res.setResponseState(1);
        } catch (Exception e) {
            e.printStackTrace();
            res.setHandOverTime(new Date());
            res.setResponseTime(0L);
            res.setResponseState(3);
            res.setResult("响应超时!");
        }
        return res;
    }
}
